package com.tuoyu.peopletest.device;

import android.widget.EditText;

import com.stresscube.usbdevice.USBDevicePDHIDWithAddress;
import com.tuoyu.peopletest.R;

public class TestPara_old {
    private final byte[] version={0x01, 0x01};
    private final int nDataBytes=7, address=0;
    private byte start, end, gain, amplify, color;

    public int getStart() {
        return start&0xFF;
    }

    public void setStart(byte start) {
        this.start = start;
    }

    public int getEnd() {
        return end&0xFF;
    }

    public void setEnd(byte end) {
        this.end = end;
    }

    public byte getGain() {
        return gain;
    }

    public void setGain(byte gain) {
        this.gain = gain;
    }

    public byte getAmplify() {
        return amplify;
    }

    public void setAmplify(byte amplify) {
        this.amplify = amplify;
    }
    public int getBrightness() {
        return ((gain&0xFF)+1)*(amplify&0xFF);
    }
    public void setBrightness(int brightness) {
        gain = 15;
        amplify = (byte) (brightness / (gain + 1));
    }

    public byte getColor() {
        return color;
    }

    public void setColor(byte color) {
        this.color = color;
    }

    public void setData(byte start, byte end, byte gain, byte amplify, byte color) {
        this.start=start;
        this.end=end;
        this.gain=gain;
        this.amplify=amplify;
        this.color=color;
    }
    public void setData(byte start, byte end, int brightness, byte color) {
        this.start=start;
        this.end=end;
        setBrightness(brightness);
        this.color=color;
    }
    public boolean readFromDevice(USBDevicePDHIDWithAddress device) {
        byte[] data=new byte[nDataBytes];
        if(!device.ReadMemoBlock(address, data))
            return false;

        int iData=0;
        if(data[iData]!=version[0]||data[iData+1]!=version[1])
            return false;

        iData+=2;
        byte start=data[iData];
        iData++;
        byte end=data[iData];
        iData++;
        byte gain=data[iData];
        iData++;
        byte amplify=data[iData];
        iData++;
        byte color=data[iData];
        if((start&0xff)<0||(start&0xff)>150)
            return false;
        if((end&0xff)<50||(end&0xff)>220)
            return false;
        if((gain&0xff)>15)
            return false;
        if((amplify&0xff)>50)
            return false;
        this.start=start;
        this.end=end;
        this.gain=gain;
        this.amplify=amplify;
        this.color=color;
        return true;
    }
    public boolean writeToDevice(USBDevicePDHIDWithAddress device) {
        try {
            byte[] data = new byte[nDataBytes];
            int iData = 0;
            data[iData] = version[0];
            data[iData+1]=version[1];
            iData += 2;
            data[iData] = start;
            iData++;
            data[iData] = end;
            iData++;
            data[iData] = gain;
            iData++;
            data[iData] = amplify;
            iData++;
            data[iData] = color;
            if (device.WriteMemoBlock(address, data))
                return true;
            return false;
        }catch(Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
